﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;
using System.Text;
using System.Data.SqlClient;

namespace Nivdal
{
    public partial class AddNewMatchSentence : System.Web.UI.Page
    {
        decimal[] Parts;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            if (ou.UserAccessLevel != ("5"))
            {
                Response.Redirect("~/Default.aspx");
            }
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.AddNewMatchSentence_PageTitle;
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void CreateNewSentence(object sender, EventArgs e)
        {
            if (EventTypeDDL.SelectedValue == "0")
            {
                ErrorLabel.Text = "Please choose eventType";
            }
            else
            {
                try
                {
                    int NumParts = int.Parse(NumPartsDDL.SelectedValue);
                    decimal EventType = decimal.Parse(EventTypeDDL.SelectedValue);
                    decimal Param1 = decimal.Parse(param1DDL.SelectedValue);
                    decimal Param2 = decimal.Parse(param2DDL.SelectedValue);
                    decimal Param3 = decimal.Parse(param3DDL.SelectedValue);
                    decimal Param4 = decimal.Parse(param4DDL.SelectedValue);

                    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                    {
                        SqlCommand cmd = new SqlCommand("Nivdal_AddNewMatchSentence", conn);
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        cmd.Parameters.Add("EventType", System.Data.SqlDbType.Decimal, 2);
                        cmd.Parameters["EventType"].Value = EventType;

                        cmd.Parameters.Add("Description", System.Data.SqlDbType.NVarChar, 256);
                        cmd.Parameters["Description"].Value = DescriptionTextBox.Text.Trim();

                        if (Param1 != 0)
                        {
                            cmd.Parameters.Add("Param1", System.Data.SqlDbType.Decimal, 12);
                            cmd.Parameters["Param1"].Value = Param1;
                        }
                        if (Param2 != 0)
                        {
                            cmd.Parameters.Add("Param2", System.Data.SqlDbType.Decimal, 12);
                            cmd.Parameters["Param2"].Value = Param2;
                        }
                        if (Param3 != 0)
                        {
                            cmd.Parameters.Add("Param3", System.Data.SqlDbType.Decimal, 12);
                            cmd.Parameters["Param3"].Value = Param3;
                        }
                        if (Param4 != 0)
                        {
                            cmd.Parameters.Add("Param4", System.Data.SqlDbType.Decimal, 12);
                            cmd.Parameters["Param4"].Value = Param4;
                        }

                        SqlDataReader reader;

                        try
                        {
                            conn.Open();
                            reader = cmd.ExecuteReader();
                            if (reader.HasRows)
                            {
                                reader.Read();
                                SentenceIdHiddenField.Value = reader.GetDecimal(0).ToString();
                            }
                        }
                        catch (Exception ex1)
                        {
                            ErrorLabel.Text = ex1.Message;
                        }
                        finally
                        {
                            conn.Close();
                        }

                        Parts = new decimal[NumParts];
                        for (int i = 0; i < NumParts; i++)
                        {
                            Parts[i] = i + 1;
                        }
                        EventTypeDDL.Enabled = false;
                        SentenceIdLabel.Text = "SentenceId: " + SentenceIdHiddenField.Value;
                        param1DDL.Enabled = false;
                        param2DDL.Enabled = false;
                        param3DDL.Enabled = false;
                        param4DDL.Enabled = false;
                        NumPartsDDL.Enabled = false;
                        SendButtonsPanel.Visible = true;
                        SentencePartsRepeater.DataSource = Parts;
                        SentencePartsRepeater.DataBind();

                    }
                }
                catch (Exception ex)
                {
                    ErrorLabel.Text = ex.Message;
                }
            }

        }

        protected void SubmitAll(object sender, EventArgs e)
        {
            string SentenceText = "";
            string Separator = "";
            bool ErrorExists = false;
            string ResultText = "";
            foreach (RepeaterItem item in SentencePartsRepeater.Items)
            {
                if (!ErrorExists)
                {
                    SentenceText = (item.FindControl("PartTextBox") as TextBox).Text.Trim();
                    Separator = (item.FindControl("SeparatorsDDL") as DropDownList).SelectedValue;

                    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                    {
                        SqlCommand cmd = new SqlCommand("Nivdal_AddNewMatchSentencePart", conn);
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        SqlDataReader reader;

                        cmd.Parameters.Add("SentenceId", System.Data.SqlDbType.Decimal, 12);
                        cmd.Parameters["SentenceId"].Value = SentenceIdHiddenField.Value;

                        cmd.Parameters.Add("Separator", System.Data.SqlDbType.Decimal, 2);
                        cmd.Parameters["Separator"].Value = Separator;

                        cmd.Parameters.Add("Text", System.Data.SqlDbType.NVarChar, 256);
                        cmd.Parameters["Text"].Value = SentenceText;

                        try
                        {
                            conn.Open();
                            reader = cmd.ExecuteReader();
                            if (reader.HasRows)
                            {
                                reader.Read();
                                ResultText += reader.GetString(0);
                                ResultText += "<br /> ";
                            }
                            else
                            {
                                ResultText += "Failed to add " + SentenceText;
                                ErrorExists = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            ResultText += ex.Message;
                            ErrorExists = true;
                        }
                        finally
                        {
                            conn.Close();
                        }

                    }
                }
                else
                {
                    ResultText += "<br /> did not deal with ";
                    ResultText += (item.FindControl("PartNumberLabel") as Label).Text;
                    ResultText += " due to error in previous parts";                    
                }
            }
            ErrorLabel.Text = ResultText;
            ClearRepeater();
        }

        protected void CancelAll(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            ErrorLabel.Text = GND.ReadNivdalStringData("SentenceId", "Nivdal_DeleteNewMatchSentence", SentenceIdHiddenField.Value);
            ClearRepeater();
        }

        private void ClearRepeater()
        {
            SentenceIdLabel.Text = "";
            EventTypeDDL.Enabled = true;
            param1DDL.Enabled = true;
            param2DDL.Enabled = true;
            param3DDL.Enabled = true;
            param4DDL.Enabled = true;
            EventTypeDDL.SelectedValue = "0";
            param1DDL.SelectedValue = "0";
            param2DDL.SelectedValue = "0";
            param3DDL.SelectedValue = "0";
            param4DDL.SelectedValue = "0";
            DescriptionTextBox.Text = "";
            NumPartsDDL.Enabled = true;
            SentencePartsRepeater.DataSource = null;
            SentencePartsRepeater.DataBind();
            SendButtonsPanel.Visible = false;
        }
             
    }
}